<template>
  <div class="historySearch">
    <p class="title" v-if="historySearch && historySearch.length > 0">
      历史搜索
    </p>
    <ul class="historyList">
      <li
        v-for="(item, index) in historySearch"
        :key="index"
        @click.stop="$emit('historysearch', item)"
      >
        <i></i>
        <div class="right">
          <span>{{ item }}</span>
          <div class="close" @click.stop="deleteHistory(index)"></div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      historySearch: [],
    };
  },
  created() {
    // 获取热搜
    this.historySearch = JSON.parse(
      window.localStorage.getItem("historysearch")
    );
  },
  methods: {
    deleteHistory(index) {
      this.historySearch.splice(index, 1);
      window.localStorage.setItem(
        "historysearch",
        JSON.stringify(this.historySearch)
      );
    },
  },
};
</script>

<style lang="less" scoped>
.historySearch {
  .title {
    padding: 15px 10px 0;
    font-size: 12px;
    line-height: 12px;
    color: #666;
  }
  .historyList {
    margin: 10px 0 7px;
    li {
      display: flex;
      align-items: center;
      height: 45px;
      i {
        margin: 0 10px;
        width: 15px;
        height: 15px;
        background-image: url();
      }
      .right {
        flex: 1;
        width: 0;
        height: 100%;
        display: flex;
        align-items: center;
        position: relative;
        span {
          display: block;
          flex: 1;
          width: 0;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        .close {
          width: 12px;
          height: 12px;
          padding: 10px;
          margin-right: 10px;
          background: url(../assets/close.png) no-repeat;
          background-size: 12px 12px;
          background-position: center;
          opacity: 0.5;
          position: relative;
          z-index: 99;
        }
        &::after {
          content: "";
          width: 100%;
          height: 100%;
          width: 200%;
          height: 200%;
          transform-origin: top left;
          transform: scale(0.5);
          position: absolute;
          z-index: 2;
          top: 0;
          left: 0;
          border-bottom: 1px solid rgba(0, 0, 0, 0.1);
        }
      }
    }
  }
}
</style>